package com.pxst.bank.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.pxst.annotations.LogOperate;
import com.pxst.bank.entity.SysBankCardGroupEntity;
import com.pxst.bank.req.BankCardGroupCreateReq;
import com.pxst.bank.req.BankCardGroupReq;
import com.pxst.bank.resp.BankCardGroupResp;
import com.pxst.bank.service.SysBankCardGroupService;
import com.pxst.merchant.entity.SysMerchantInfo;
import com.pxst.resp.ApiResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.ValidationException;
import java.util.List;

/**
* @author zarke
* @since 2023-09-25 17:42:14
 * @description 银行账户管理-卡群相关接口
*/
@RestController
@RequestMapping("/api/cardGroup")
@Api(tags = "卡群相关接口")
public class SysBankCardGroupController {

    @Autowired
    private SysBankCardGroupService sysBankCardGroupService;

    @GetMapping("/simple")
    @ApiOperation(value = "群组下拉框信息")
    public ApiResp<BankCardGroupResp> getGroupInfo(@RequestParam("with") String with ) {
        return sysBankCardGroupService.getInfo();
    }

    @GetMapping
    @ApiOperation(value = "账户群组搜索")
    public ApiResp<SysBankCardGroupEntity> getGroupList(BankCardGroupReq bankCardGroupReq) {
        return sysBankCardGroupService.getGroupList(bankCardGroupReq);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "账户群组详情")
    public ApiResp<SysBankCardGroupEntity> getGroupById(Long id) {
        return ApiResp.sucess(sysBankCardGroupService.getById(id));
    }

    @PostMapping
    @ApiOperation(value = "新增群组")
    @LogOperate(operate = "新增群组", service = SysBankCardGroupService.class, type = "add")
    @PreAuthorize("@ss.hasAnyPermissions('cardGroup:list:create')")
    public ApiResp<SysBankCardGroupEntity> create(@Validated @RequestBody BankCardGroupCreateReq bankCardGroupCreateReq) {
        SysBankCardGroupEntity one = sysBankCardGroupService.getOne(new LambdaQueryWrapper<SysBankCardGroupEntity>()
                .eq(SysBankCardGroupEntity::getGroupName, bankCardGroupCreateReq.getGroupName()));
        if (!ObjectUtils.isEmpty(one)) {
            throw new ValidationException("群组名称不能重复！！！");
        }
        SysBankCardGroupEntity sysBankCardGroupEntity = new SysBankCardGroupEntity();
        BeanUtils.copyProperties(bankCardGroupCreateReq,sysBankCardGroupEntity);
        sysBankCardGroupService.save(sysBankCardGroupEntity);
        return ApiResp.sucess(sysBankCardGroupService.getById(sysBankCardGroupEntity.getId()));
    }

    @GetMapping("/{id}/merchant/all")
    @ApiOperation(value = "账户群组下所有商户信息")
    public ApiResp<List<SysMerchantInfo>> getMerchantAll(@PathVariable Long id) {
        return sysBankCardGroupService.getMerchantAll(id);
    }

    /*@GetMapping("/{id}/bankcard/all")
    @ApiOperation(value = "账户群组下所有银行账户信息")
    public ApiResp<List<SysMerchantInfo>> getBankcardAll(@RequestParam("cardGroup") Long cardGroup ,@RequestParam("type") Integer type) {
        return sysBankCardGroupService.getBankcardAll(cardGroup,type);
    }*/
}
